package com.jlkj.web.shopnew.service.impl;

import cc.s2m.web.utils.webUtils.service.BaseServiceImpl;
import cc.s2m.web.utils.webUtils.vo.Expressions;
import com.google.common.collect.Lists;
import com.jlkj.web.shopnew.constant.ConstantDateType;
import com.jlkj.web.shopnew.dao.OrdersMapper;
import com.jlkj.web.shopnew.dto.DateDto;
import com.jlkj.web.shopnew.enums.EnumOrderStatus;
import com.jlkj.web.shopnew.pojo.Managers;
import com.jlkj.web.shopnew.pojo.Orders;
import com.jlkj.web.shopnew.request.GetUserInfoRequest;
import com.jlkj.web.shopnew.request.StatiscRequest;
import com.jlkj.web.shopnew.service.IManagers;
import com.jlkj.web.shopnew.service.IOrders;
import com.jlkj.web.shopnew.service.IStaff;
import com.jlkj.web.shopnew.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrdersImpl extends BaseServiceImpl<Orders, OrdersMapper, Integer> implements IOrders {
    @Autowired
    private OrdersMapper ordersMapper;

    protected OrdersMapper getDao() {
        return ordersMapper;
    }

    @Autowired
    private IStaff staffService;

    @Autowired
    private IManagers managersService;

    @Override
    public Integer getOrdersCount(StatiscRequest statiscRequest) {

        int mgId = managersService.getMgIdByUid(statiscRequest.getUid());

        List listStatus = Lists.newArrayList(
                EnumOrderStatus.WAIT_PAY.getCode(),
                EnumOrderStatus.WAIT_SEND.getCode(),
                EnumOrderStatus.WAIT_RECEIVING.getCode(),
                EnumOrderStatus.SUCCESS.getCode(),
                EnumOrderStatus.AFTER_SALE.getCode()
        );

        DateDto dateDto = DateUtil.getDayByType(statiscRequest.getDateType(), statiscRequest.getStartDay(),
                statiscRequest.getEndDay());
        Orders condi = new Orders();

        /*if (statiscRequest.isBoss()) {
            List<Integer> list = staffService.getCompanyStaff(statiscRequest.getUid());
            condi.and(Expressions.in("uid", list));

        } else {*/
            condi.setMgId(String.valueOf(mgId));
        //}

        condi.and(Expressions.in("status", listStatus));
        condi.and(Expressions.le("update_time",
                DateUtil.getLastSecIntegralTime(dateDto.getEndDay())));
        condi.and(Expressions.ge("update_time",
                DateUtil.getFirstSecIntegralTime(dateDto.getStartDay())));

        int count = this.getCount(condi);

        return count;
    }

    @Override
    public Integer getOrdersCount(StatiscRequest statiscRequest, DateDto dateDto) {
        int mgId = managersService.getMgIdByUid(statiscRequest.getUid());


        List listStatus = Lists.newArrayList(
                EnumOrderStatus.WAIT_PAY.getCode(),
                EnumOrderStatus.WAIT_SEND.getCode(),
                EnumOrderStatus.WAIT_RECEIVING.getCode(),
                EnumOrderStatus.SUCCESS.getCode(),
                EnumOrderStatus.AFTER_SALE.getCode()
        );

        Orders condi = new Orders();

        /*if (statiscRequest.isBoss()) {
            List<Integer> list = staffService.getCompanyStaff(statiscRequest.getUid());
            condi.and(Expressions.in("uid", list));

        } else {*/
            condi.setMgId(String.valueOf(mgId));
        //}

        condi.and(Expressions.in("status", listStatus));

        if (ConstantDateType.ALL_DAY.equals(statiscRequest.getDateType())){
            condi.and(Expressions.le("update_time",
                    DateUtil.getTime(DateUtil.getLastSecIntegralTime(dateDto.getEndDay()))));
        }else {
            if (dateDto.getEndDay() != null) {
                condi.and(Expressions.le("update_time",
                       DateUtil.getTime( DateUtil.getLastSecIntegralTime(dateDto.getEndDay()))));
            }

            if (dateDto.getStartDay() != null) {
                condi.and(Expressions.ge("update_time",
                       DateUtil.getTime( DateUtil.getFirstSecIntegralTime(dateDto.getStartDay()))));
            }

        }


        int count = this.getCount(condi);

        return count;
    }

    @Override
    public Integer getOrdersCount(int uid, DateDto dateDto) {

        int mgId = managersService.getMgIdByUid(uid);
        List listStatus = Lists.newArrayList(
                EnumOrderStatus.WAIT_PAY.getCode(),
                EnumOrderStatus.WAIT_SEND.getCode(),
                EnumOrderStatus.WAIT_RECEIVING.getCode(),
                EnumOrderStatus.SUCCESS.getCode(),
                EnumOrderStatus.AFTER_SALE.getCode()
        );
        Orders condi = new Orders();
        condi.setMgId(String.valueOf(mgId));
        condi.and(Expressions.in("status", listStatus));
        condi.and(Expressions.le("update_time",
               DateUtil.getTime( DateUtil.getLastSecIntegralTime(dateDto.getEndDay()))));
        condi.and(Expressions.ge("update_time",
              DateUtil.getTime(  DateUtil.getFirstSecIntegralTime(dateDto.getStartDay()))));
        int count = this.getCount(condi);
        return count;
    }

    @Override
    public BigDecimal getRealPriceCount(StatiscRequest statiscRequest,DateDto dateDto) {
        int mgId = managersService.getMgIdByUid(statiscRequest.getUid());


        List listStatus = Lists.newArrayList(
                EnumOrderStatus.WAIT_SEND.getCode(),
                EnumOrderStatus.WAIT_RECEIVING.getCode(),
                EnumOrderStatus.SUCCESS.getCode(),
                EnumOrderStatus.AFTER_SALE.getCode()
        );
        Orders condi = new Orders();

        /*if (statiscRequest.isBoss()) {
            List<Integer> list = staffService.getCompanyStaff(statiscRequest.getUid());
            condi.and(Expressions.in("uid", list));

        } else {*/
            condi.setMgId(String.valueOf(mgId));
        //}

        condi.and(Expressions.in("status", listStatus));
        if (ConstantDateType.ALL_DAY.equals(statiscRequest.getDateType())){
            condi.and(Expressions.le("update_time",
                    DateUtil.getTime(  DateUtil.getLastSecIntegralTime(dateDto.getEndAddDay()))));
        }else {
            if (dateDto.getEndDay() != null) {
                condi.and(Expressions.le("update_time",
                        DateUtil.getTime(   DateUtil.getLastSecIntegralTime(dateDto.getEndAddDay()))));
            }

            if (dateDto.getStartDay() != null) {
                condi.and(Expressions.ge("update_time",
                        DateUtil.getTime(   DateUtil.getFirstSecIntegralTime(dateDto.getStartAddDay()))));
            }

        }


        Map<String, ?> maxMap = this.aggregate(condi, new String[]{"sum"}, new String[]{"real_price"});

        BigDecimal sumPrice = BigDecimal.ZERO;

        if (maxMap != null) {
            sumPrice = (BigDecimal) maxMap.get("sum_real_price");
        }

        return sumPrice;
    }

    @Override
    public Integer getOrdersAddCount(StatiscRequest statiscRequest, DateDto dateDto) {
        int mgId = managersService.getMgIdByUid(statiscRequest.getUid());


        List listStatus = Lists.newArrayList(
                EnumOrderStatus.WAIT_PAY.getCode(),
                EnumOrderStatus.WAIT_SEND.getCode(),
                EnumOrderStatus.WAIT_RECEIVING.getCode(),
                EnumOrderStatus.SUCCESS.getCode(),
                EnumOrderStatus.AFTER_SALE.getCode()
        );

        Orders condi = new Orders();

        /*if (statiscRequest.isBoss()) {
            List<Integer> list = staffService.getCompanyStaff(statiscRequest.getUid());
            condi.and(Expressions.in("uid", list));

        } else {*/
            condi.setMgId(String.valueOf(mgId));
        //}

        condi.and(Expressions.in("status", listStatus));

        if (ConstantDateType.ALL_DAY.equals(statiscRequest.getDateType())){
            condi.and(Expressions.le("update_time",
                    DateUtil.getTime(  DateUtil.getLastSecIntegralTime(dateDto.getEndAddDay()))));
        }else {
            if (dateDto.getEndDay() != null) {
                condi.and(Expressions.le("update_time",
                        DateUtil.getTime(   DateUtil.getLastSecIntegralTime(dateDto.getEndAddDay()))));
            }

            if (dateDto.getStartDay() != null) {
                condi.and(Expressions.ge("update_time",
                        DateUtil.getTime(   DateUtil.getFirstSecIntegralTime(dateDto.getStartAddDay()))));
            }

        }


        int count = this.getCount(condi);

        return count;
    }

    @Override
    public BigDecimal getRealPriceAddCount(StatiscRequest statiscRequest, DateDto dateDto) {
        int mgId = managersService.getMgIdByUid(statiscRequest.getUid());


        List listStatus = Lists.newArrayList(
                EnumOrderStatus.WAIT_SEND.getCode(),
                EnumOrderStatus.WAIT_RECEIVING.getCode(),
                EnumOrderStatus.SUCCESS.getCode(),
                EnumOrderStatus.AFTER_SALE.getCode()
        );
        Orders condi = new Orders();

        /*if (statiscRequest.isBoss()) {
            List<Integer> list = staffService.getCompanyStaff(statiscRequest.getUid());
            condi.and(Expressions.in("uid", list));

        } else {*/
            condi.setMgId(String.valueOf(mgId));
        //}

        if (ConstantDateType.ALL_DAY.equals(statiscRequest.getDateType())){
            condi.and(Expressions.le("update_time",
                  DateUtil.getTime(  DateUtil.getLastSecIntegralTime(dateDto.getEndAddDay()))));
        }else {
            if (dateDto.getEndDay() != null) {
                condi.and(Expressions.le("update_time",
                        DateUtil.getTime(    DateUtil.getLastSecIntegralTime(dateDto.getEndAddDay()))));
            }

            if (dateDto.getStartDay() != null) {
                condi.and(Expressions.ge("update_time",
                        DateUtil.getTime(   DateUtil.getFirstSecIntegralTime(dateDto.getStartAddDay()))));
            }

        }
        condi.and(Expressions.in("status", listStatus));


        Map<String, ?> maxMap = this.aggregate(condi, new String[]{"sum"}, new String[]{"real_price"});

        BigDecimal sumPrice = BigDecimal.ZERO;

        if (maxMap != null) {
            sumPrice = (BigDecimal) maxMap.get("sum_real_price");
        }

        return sumPrice;
    }

    @Override
    public int getOrders(GetUserInfoRequest getUserInfoRequest) {
        int mgId = managersService.getMgIdByUid(getUserInfoRequest.getOperationId());
        List listStatus = Lists.newArrayList(
                EnumOrderStatus.WAIT_PAY.getCode(),
                EnumOrderStatus.WAIT_SEND.getCode(),
                EnumOrderStatus.WAIT_RECEIVING.getCode(),
                EnumOrderStatus.SUCCESS.getCode(),
                EnumOrderStatus.AFTER_SALE.getCode()
        );
        Orders condi = new Orders();
        condi.setMgId(String.valueOf(mgId));
        condi.setUid(getUserInfoRequest.getUid());
        condi.and(Expressions.in("status", listStatus));

        int count = this.getCount(condi);
        return count;
    }
}